package model.location;

import java.util.ArrayList;
import java.util.Arrays;

import model.Dimension;
import model.OnScreen;
import android.util.Log;

public class WorldLocationCalculator 
{
	private static final String TAG = WorldLocationCalculator.class.getSimpleName();
	
	public static void calculateSearchArea(ArrayList<ReferencePoint> refPoints) //throws Exception
	{
		if(refPoints.size() == 1)
		{
			calculateSearchArea(refPoints.get(0));
			return;
		}
		
		//Gets 2 codes
		ReferencePoint firstInArray = refPoints.get(0);
		ReferencePoint secondInArray = refPoints.get(1);
	
		//calculate centerpoints of codes
		float centerX1 = firstInArray.getScreenArea().getCenterCoord().getX();
		float centerY1 = firstInArray.getScreenArea().getCenterCoord().getY();
		
		float centerX2 = secondInArray.getScreenArea().getCenterCoord().getX();
		float centerY2 = secondInArray.getScreenArea().getCenterCoord().getY();
		
		//calculate distance between codes in pixels
		float xScreenDistance = centerX2 - centerX1;
		float yScreenDistance = centerY2 - centerY1;
		
		//calculate actual distance between codes in cm
		float xWorldDistance = secondInArray.getWorldCoordinate().getX() - firstInArray.getWorldCoordinate().getX();
		float yWorldDistance = firstInArray.getWorldCoordinate().getY() - secondInArray.getWorldCoordinate().getY();
		
		if(centerX1 < centerX2)
			setLeftToRightScale(firstInArray.getScreenArea().getDimension().getWidth(), secondInArray.getScreenArea().getDimension().getWidth());
		else
			setLeftToRightScale(secondInArray.getScreenArea().getDimension().getWidth(), firstInArray.getScreenArea().getDimension().getWidth());
		
		
		float scaleX = xWorldDistance / xScreenDistance;
		
		@SuppressWarnings("unused") // Might be useful later
		float scaleY = yWorldDistance / yScreenDistance;
		
		Dimension screen = OnScreen.getInstance().getPreviewDimension();
		
		float worldUpperLeftX = (firstInArray.getWorldCoordinate().getX() - (centerX1 * scaleX));
		float worldUpperLeftY = (firstInArray.getWorldCoordinate().getY() + (centerY1 * scaleX));
		float worldUpperLeftZ = firstInArray.getWorldCoordinate().getZ();
		
		float worldLowerRightX = (float) (secondInArray.getWorldCoordinate().getX() + ((screen.getWidth() - centerX2) * scaleX));
		float worldLowerRightY = (float) (secondInArray.getWorldCoordinate().getY() - ((screen.getHeight() - centerY2) * scaleX));
		float worldLowerRightZ = secondInArray.getWorldCoordinate().getZ();
		
		WorldCoordinate upperLeftCoord = new WorldCoordinate(worldUpperLeftX,worldUpperLeftY,worldUpperLeftZ);
		
		float width = worldLowerRightX - worldUpperLeftX;
		float height = worldUpperLeftY - worldLowerRightY;
		float z = (worldUpperLeftZ + worldLowerRightZ) / 2f;
		
		SearchArea area = new SearchArea(upperLeftCoord, new Dimension(width, height), (int) z);
		
		OnScreen.getInstance().setSearchArea(area);
	}
	
	private static void setLeftToRightScale(float height1, float height2)
	{
		float leftToRightScale = (height1 / height2);
		OnScreen.getInstance().setLeftToRightCodeScale(leftToRightScale);
		
		Log.d(TAG, String.format("new scale: %f . made from left height: %f  and right height: %f", 
				leftToRightScale, height1, height2));
	}
	
	private static void calculateSearchArea(ReferencePoint referencePoint) 
	{
		float screenWidth = referencePoint.getScreenArea().getDimension().getWidth();
		float worldWidth = referencePoint.getWorldArea().getDimension().getWidth();
		
		float scale = worldWidth / screenWidth;
		
		Log.d(TAG, String.format("screenWidth: %f -- scale: %f", screenWidth, (float) scale));
		
		//calculate centerpoints of codes
		float centerX = referencePoint.getScreenArea().getCenterCoord().getX();
		float centerY = referencePoint.getScreenArea().getCenterCoord().getY();
		
		float ulx = (referencePoint.getWorldCoordinate().getX() - (centerX * scale));
		float uly = (referencePoint.getWorldCoordinate().getY() + (centerY * scale));
		float ulz = referencePoint.getWorldCoordinate().getZ();
		
		Dimension screen = OnScreen.getInstance().getPreviewDimension();
		
		float llx = (referencePoint.getWorldCoordinate().getX() + ((screen.getWidth() - centerX) * scale));
		float lly = (referencePoint.getWorldCoordinate().getY() - ((screen.getHeight() - centerY) * scale));
		//float llz = referencePoint.getWorldCoordinate().getZ();
		

		WorldCoordinate upperLeftCoord = new WorldCoordinate(ulx,uly,ulz);
		//WorldCoordinate lowerRightCoord = new WorldCoordinate(llx,lly,llz);
		
		float width = llx - ulx;
		float height = uly - lly;
		
		SearchArea area = new SearchArea(upperLeftCoord, new Dimension(width, height), (int) ulz);
		Log.d(TAG, Arrays.toString(area.getPoints()));
		
		OnScreen.getInstance().setSearchArea(area);
	}
}
